/*
 * $RCSfile: HTMLStringConverter.java,v $
 * $Revision: 32.5 $
 *
 * Comments:
 *
 * (C) Copyright Biuro-Technika 1996.  All rights reserved.
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Biuro-Technika
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 *
 * $Author: michal $          $Locker:  $
 * $Date: 2010/06/12 20:51:32 $
 * $Log: HTMLStringConverter.java,v $
 * Revision 32.5  2010/06/12 20:51:32  michal
 * optymalizacja petli
 *
 * Revision 32.4  2009/09/22 12:09:10  michal
 * ParaSoft -> Biuro-Technika
 *
 * Revision 32.3  2008/12/09 08:41:44  michal
 * dodanie obslugi znakow: -.
 *
 * Revision 32.2  2004/03/03 15:00:09  limbur
 * Obsluga Java Script. Lepsze nazewnictwo funkcji.
 *
 * Revision 32.1  2003/09/29 09:30:17  limbur
 * Original source code.
 *
 */

package parasoft.centrap.utils;

public class HTMLStringConverter
{
    private String str;

    public HTMLStringConverter(String str)
    {
        this.str = str;
    }

    public String get()
    {
        return str;
    }

    public String convert()
    {
        return convert(str);
    }

    public String invert()
    {
        return invert(str);
    }

    public String getJavaScriptString()
    {
        return getJavaScriptString(str);
    }

    public String toString()
    {
        return convert();
    }

    public static String convert(String str)
    {
        if(str == null) {
            return "";
        }

        StringBuffer buff = new StringBuffer(str);
        int index = -1;
        while((index = buff.toString().indexOf(' ')) >= 0) {
        	buff.replace(index, index + 1, "&#32;");
        }
        while((index = buff.toString().indexOf('\"')) >= 0) {
            buff.replace(index, index + 1, "&#34;");
        }
        while((index = buff.toString().indexOf('\'')) >= 0) {
            buff.replace(index, index + 1, "&#39;");
        }
        while((index = buff.toString().indexOf('-')) >= 0) {
        	buff.replace(index, index + 1, "&#45;");
        }
        while((index = buff.toString().indexOf('.')) >= 0) {
        	buff.replace(index, index + 1, "&#46;");
        }
        return buff.toString().trim();
    }

    public static String invert(String str)
    {
        if(str == null) {
            return "";
        }

        StringBuffer buff = new StringBuffer(str.trim());
        int index = -1;
        while((index = buff.toString().indexOf("&#32")) >= 0) {
        	buff.replace(index, index + 5, " ");
        }
        while((index = buff.toString().indexOf("&#34;")) >= 0) {
            buff.replace(index, index + 5, "\"");
        }
        while((index = buff.toString().indexOf("&#39;")) >= 0) {
            buff.replace(index, index + 5, "\'");
        }
        while((index = buff.toString().indexOf("&#45;")) >= 0) {
        	buff.replace(index, index + 5, "-");
        }
        while((index = buff.toString().indexOf("&#46;")) >= 0) {
        	buff.replace(index, index + 5, ".");
        }
        return buff.toString();
    }

    public static String getJavaScriptString(String str)
    {
        if(str == null) {
            return "";
        }

        StringBuffer buff = new StringBuffer(str.length());
        for(int i = 0, len = str.length() ; i < len ; i++) {
            if(str.charAt(i) == '\\') {
                buff.append("\\\\");
            }
            else if(str.charAt(i) == '\"') {
                buff.append("\\\"");
            }
            else if(str.charAt(i) == '\'') {
                buff.append("\\\'");
            }
            else if(str.startsWith("&#34;", i)) {
                buff.append("\\\"");
                i += 4;
            }
            else if(str.startsWith("&#39;", i)) {
                buff.append("\\\'");
                i += 4;
            }
            else {
                buff.append(str.charAt(i));
            }
        }
        return buff.toString().trim();
    }

}
